﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;

namespace gui {

    /// <summary>
    /// GUI Notifiable ConsoleConnect Class
    /// Contains contents and isActive
    /// </summary>
    public class ConsoleConnect : DependencyObject {

        public static ConsoleConnect Instance;
        public static Dispatcher _Dispatcher { get; set; }

        public static DependencyProperty ContentsProperty = DependencyProperty.Register("Contents", typeof(string), typeof(ConsoleConnect));
        public static DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool), typeof(ConsoleConnect));

        static ConsoleConnect() {
            Instance = new ConsoleConnect();
        }

        public bool IsActive {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }
        public string Contents {
            get { return (string)GetValue(ContentsProperty); }
            set { SetValue(ContentsProperty, value); }
        }

        public static void Write(string str) {
            if (_Dispatcher != null && !str.Equals(null)) {
                _Dispatcher.BeginInvoke(
                    DispatcherPriority.Send,
                    new Action(delegate() {
                        Instance.Contents = String.Concat(Instance.Contents, str);
                        Instance.IsActive = true;
                }));
            }
        }

        public static void WriteLine(string str) {
            if (_Dispatcher != null && !str.Equals(null)) {
                _Dispatcher.BeginInvoke(
                    DispatcherPriority.Send,
                    new Action(delegate() {
                        Instance.Contents = String.Join("", Instance.Contents, str, "\n");
                        Instance.IsActive = true;
                }));
            }
        }

        public static void Clear() {
            if (_Dispatcher != null) {
                _Dispatcher.BeginInvoke(
                    DispatcherPriority.Send,
                    new Action(delegate() {
                        Instance.Contents = string.Empty;
                        Instance.IsActive = false;
                    }));
            }
        }

    }
}
